package com.xieyong.leetcode;

import java.util.Arrays;

public class Leetcode2966 {
    public static void main(String[] args) {
        Leetcode2966 leetcode2966 = new Leetcode2966();
        int[] nums = {4,2,9,8,2,12,7,12,10,5,8,5,5,7,9,2,5,11};
        int k = 14;
        int[][] ints = leetcode2966.divideArray(nums, k);
        for (int i = 0; i < ints.length; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(ints[i][j] + ",");
            }
            System.out.println();
        }
    }


    public int[][] divideArray(int[] nums, int k) {
        Arrays.sort(nums);
        int[][] res = new int[nums.length / 3][3];
        int[][] errorRes = new int[0][0];
        int index = 0;
        for (int i = 0; i < nums.length - 2; i+= 3) {
            if (nums[i + 2] - nums[i] > k) {
                return errorRes;
            } else {
                res[index][0] = nums[i];
                res[index][1] = nums[i + 1];
                res[index][2] = nums[i + 2];
                index++;
            }
        }
        return res;
    }
}
